package mod.luckymod;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.simibubi.create.foundation.data.CreateRegistrate;
import mod.luckymod.effects.TetanusEffect;
import mod.luckymod.fluids.AllFluids;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

// The value here should match an entry in the META-INF/mods.toml file
@Mod(Luckymod.MODID)
public class Luckymod {
    public static ResourceLocation asResource(String path) {
        return new ResourceLocation(MODID, path);
    }
    public static ScheduledExecutorService scheduler;
    // Define mod id in a common place for everything to reference
    public static final String MODID = "luckymod";
    private static final CreateRegistrate REGISTRATE = CreateRegistrate.create(MODID);
    private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
    public static CreateRegistrate registrate() {
        if (!STACK_WALKER.getCallerClass().getPackageName().startsWith("mod.luckymod"))
            throw new UnsupportedOperationException("其他模组不能使用lucky模组的注册方法");
        return REGISTRATE;
    }
    public Luckymod() {
        ModBlocks.register();
        ModItems.register();
        ModEntities.register();
        AllFluids.register();
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        REGISTRATE.registerEventListeners(modEventBus);
        MinecraftForge.EVENT_BUS.register(this);
        AllCreativeModeTabs.register(modEventBus);
        MinecraftForge.EVENT_BUS.register(new ServerEvents());
        // 注册事件监听器
        TetanusEffect.registerEffects(modEventBus);
        modEventBus.addListener(this::registerRenderers);
        modEventBus.addListener(this::onClientSetup);
    }
    @SubscribeEvent
    public void registerRenderers(EntityRenderersEvent.RegisterRenderers event) {
        event.registerEntityRenderer(ModEntities.SodiumEntity.get(), SodiumEntityRenderer::new);
    }
    @SubscribeEvent
    public void onClientSetup(FMLCommonSetupEvent event)
    {
        event.enqueueWork(AllFluids::registerFluidInteractions);
    }
    public static class ServerEvents {
        @SubscribeEvent
        public void onServerStart(ServerStartingEvent event) {
            scheduler = new ScheduledThreadPoolExecutor(
                    1,
                    new ThreadFactoryBuilder().setNameFormat("my-mod-scheduler-%d").build(),
                    new ThreadPoolExecutor.AbortPolicy()
            );
        }
        @SubscribeEvent
        public void onServerStop(ServerStoppingEvent event) {
            if (scheduler != null) {
                scheduler.shutdown();
                try {
                    if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) {
                        scheduler.shutdownNow();
                    }
                } catch (InterruptedException e) {
                    scheduler.shutdownNow();
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
}
